//
//  GDAboutController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/2/26.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//


/*
  此界面为关于界面。采用的是bmob动态改变图片的内容。
  动态配置图片

 
 */

#import "GDAboutController.h"
#import "GDAboudImageModel.h"
#import <BmobSDK/Bmob.h>
#import <UIImageView+WebCache.h>
@interface GDAboutController ()<UIScrollViewDelegate>{
    int _lastPosition;
    int textIndex;
    BOOL showImage;
}

@property (nonatomic,strong) NSMutableArray * imageArray;
@property(nonatomic,weak) UIScrollView * backscrollView;
@property (nonatomic,strong) NSMutableArray * labelTextArray;
@end

@implementation GDAboutController

- (NSMutableArray *)labelTextArray{
    if (!_labelTextArray) {
        _labelTextArray = [NSMutableArray array];
    }
    return _labelTextArray;
}


- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _lastPosition = 0;
    textIndex = 0;
    showImage = true;
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadImageData];
    [self setNavigatorControl];
}

- (void) setNavigatorControl
{
    CGRect aScreenRect = [[UIScreen mainScreen] bounds];
    
    //创建navbar
    UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, aScreenRect.size.width, 66)];
    //创建navbaritem
    UINavigationItem *NavTitle = nil;
    
    NSString *nvTitle = @"关于这款App";
    
    NavTitle = [[UINavigationItem alloc] initWithTitle: nvTitle];
    ;
    [nav pushNavigationItem:NavTitle animated:YES];
    [self.view addSubview:nav];
    //创建barbutton 创建系统样式的
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left-cross"] style:UIBarButtonItemStylePlain target:self action:@selector(rtnExit)];
    
    
    //设置barbutton
    NavTitle.leftBarButtonItem = backItem;
    [nav setItems:[NSArray arrayWithObject:NavTitle]];
}

- (void) rtnExit{
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark *********************加载数据*********************
- (void) loadImageData{
    BmobQuery * bquery  = [BmobQuery queryWithClassName:@"GDAbout_t"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (array.count == 0) {
            return;
        }
        int ID = 0;
        for (BmobObject *object in array) {
            
            if ([object objectForKey:@"ID"]) {
                 ID = (int)[object objectForKey:@"ID"];
                GDLog(@"ID: %d",ID);
            }
            NSString *imgFile = @"";
            if ([object objectForKey:@"imgSrcData"]) {
               imgFile  = [object objectForKey:@"imgSrcData"];
                GDLog(@"imgSrcData: %@",imgFile);
                
            }
            NSString *text = @"";
            if ([object objectForKey:@"text"]) {
                text  = [object objectForKey:@"text"];
                GDLog(@"text: %@",text);
                
            }
            GDAboudImageModel * imageModel = [GDAboudImageModel imageDataWithVar:ID and:imgFile text:text];
            [self.imageArray addObject:imageModel];
        }
        NSString * imageFilePath = [[NSBundle mainBundle] pathForResource:@"myCode.png" ofType:nil];
        GDAboudImageModel * imageModel = [GDAboudImageModel imageDataWithVar:(ID + 1) and:imageFilePath text:@"我喜欢产品,但是也不排除外包作品,喜欢做“外卖”haha"];
        [self.imageArray addObject:imageModel];
        [self loadImageUI];
       
    }];
}


#pragma mark *********************更新UI*********************
- (void) loadImageUI{
    GDLog(@"模型数量 %ld",self.imageArray.count);
    // 放置在底侧 中
    // 滑动界面
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,66, GDScreenSize.size.width, GDScreenSize.size.height )];
    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(0, GDScreenSize.size.height * self.imageArray.count);
    scrollView.bounces = YES;
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    self.backscrollView = scrollView;
    for (int index = 0; index < self.imageArray.count; index ++) {
        UIImageView *  imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, index * self.backscrollView.frame.size.height  ,GDScreenSize.size.width ,GDScreenSize.size.height )];
        
        GDAboudImageModel * model = self.imageArray[index];
        if ([model.imgSrcData hasPrefix:@"http://"]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.imgSrcData]];
        }else{
            imageView.image = [UIImage imageWithContentsOfFile:model.imgSrcData];
        }
        [self.backscrollView addSubview:imageView];
    }
 
    [self showText:textIndex direction:DIRECTION_NO];

}


/*
    开始滚动并超过一半的时候，就开始移除文字
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
       GDLog(@"%s",__func__);
    if (self.labelTextArray.count == 0) {
        return;
    }
    if( scrollView.contentOffset.x - scrollView.frame.size.height / 2){
            // 开始移除
        for (UILabel * labelStr in self.labelTextArray) {
            [labelStr removeFromSuperview];
        }
    }

}
#pragma mark 即将要开始滚动的时候的回调
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
     GDLog(@"%s",__func__);

}

#pragma mark 滚动完成的回调
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    showImage = true;
    GDLog(@"%s",__func__);
        int currentPostion = scrollView.contentOffset.y;
        if (currentPostion - _lastPosition > 25) {
            _lastPosition = currentPostion;
            GDLog(@"currentPostion  向上");
            if (textIndex + 1 >= self.imageArray.count) {
                return;
            }
            [self showText:textIndex += 1 direction:DIRECTION_UP];
       }
        else if (_lastPosition - currentPostion > 25){
            _lastPosition = currentPostion;
            GDLog(@"_lastPosition 向下");
            if (textIndex - 1 < 0) {
                return;
            }
             [self showText:textIndex -= 1 direction:DIRECTION_DOWN];
        }
 
}



- (void) showText : (int ) index direction:(DIRECTION) dir{
    // 显示文本
    UILabel * lableView = [[UILabel alloc] init];
    lableView.alpha = 0.4;
    lableView.contentMode = UIViewContentModeLeft;
    lableView.numberOfLines = MAXFLOAT;
    GDAboudImageModel *model = self.imageArray[index];
    lableView.text = model.text;
    [self.view  addSubview:lableView];
    if (dir == DIRECTION_UP || dir == DIRECTION_NO) {
        lableView.frame =  CGRectMake(GDScreenSize.size.width * 0.05, GDScreenSize.size.height * 0.8, 200,200);
    }else {
        lableView.frame =  CGRectMake(GDScreenSize.size.width * 0.95 - 200, GDScreenSize.size.height * 0.3, 200,200);
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        lableView.alpha = 1;
        lableView.frame = CGRectMake(GDScreenSize.size.width * 0.1, GDScreenSize.size.height * 0.3, 200, 230);
    }];
    [self.labelTextArray addObject:lableView];

}
- (BOOL)prefersStatusBarHidden{
    return YES;
}

@end
